package com.taotao.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyNoticeMapper;
import com.taotao.mapper.EyOfficialAccountsMapper;
import com.taotao.pojo.EyNotice;
import com.taotao.pojo.EyNoticeExample;
import com.taotao.pojo.EyOfficialAccounts;
import com.taotao.pojo.EyOfficialAccountsExample;
import com.taotao.pojo.EyOfficialAccountsExample.Criteria;
import com.taotao.service.NoticeService;

@Service
public class NoticeServiceImpl implements NoticeService {

	@Autowired
	EyNoticeMapper eyNoticeMapper;
	@Autowired
	EyOfficialAccountsMapper eyOfficialAccountsMapper;
	
	BaseResult baseResult = new BaseResult();

	@Override
	public Map<String, Object> getNoticeList(Map<String, String> map) {
		try {
			String type=map.get("type");
			String memberId=map.get("memberId");
			String subscribe="0"; //未关注
			if(StringUtils.isNotBlank(memberId)){
				EyOfficialAccountsExample exampleOff = new EyOfficialAccountsExample();
				Criteria criteriaOff = exampleOff.createCriteria();
				criteriaOff.andMemberIdEqualTo(Integer.parseInt(memberId));
				List<EyOfficialAccounts> accountsList = eyOfficialAccountsMapper.selectByExample(exampleOff);
				if(accountsList!=null && accountsList.size()>0){
					EyOfficialAccounts accounts = accountsList.get(0);
					subscribe = accounts.getSubscribe();
				}
			}
			
			EyNoticeExample example = new EyNoticeExample();
			com.taotao.pojo.EyNoticeExample.Criteria criteria = example.createCriteria();
			if(StringUtils.isNotBlank(type)){
				criteria.andTypeEqualTo(type);
			}
			if(Constants.num_1.equals(subscribe)){ //已关注
				criteria.andIdNotEqualTo(Integer.parseInt(Constants.num_1));
			}
			criteria.andStatusEqualTo(Constants.num_1); //正常
			example.setOrderByClause(" create_time desc ");
			List<EyNotice> list = eyNoticeMapper.selectByExample(example);
			for(EyNotice n:list){
				String createTime=n.getCreateTime();
				createTime=DateUtil.getDateTimeFormatStr(createTime);
				n.setCreateTime(createTime);
			}
			
			return baseResult.getSuccMap(list);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

}
